package com.medord.controller;

import com.alibaba.fastjson.JSONObject;
import com.medord.dao.StudentDao;
import com.medord.entity.Student;
import com.medord.entity.StudentAdepter;
import com.medord.machine.core.FlowConstant;
import com.medord.machine.core.IFlowContext;
import com.medord.machine.core.entity.FlowResult;
import com.medord.machine.core.entity.ICurrentStateDto;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 孙丰佳
 * @version V1.0
 * @Title: MainController
 * @Package com.medord.controller
 * @Description: (用一句话描述该文件做什么)
 * @date 2021-08-01 15:44
 **/
@Controller
public class MainController {

    @Autowired
    private IFlowContext context;

    @Autowired
    private StudentDao studentDao;

    @SneakyThrows
    @RequestMapping("test")
    @ResponseBody
    public FlowResult test(String state) {
        List<ICurrentStateDto> dtoList = new ArrayList<>();
        List<Student> studentList = studentDao.getAll();
        for (Student student : studentList) {
            StudentAdepter studentAdepter = new StudentAdepter(student);
            dtoList.add(studentAdepter);
        }
        return context.forwordState(FlowConstant.MED_FLOW_NAME, dtoList, state, null);
    }

    @SneakyThrows
    @RequestMapping("test1")
    @ResponseBody
    public FlowResult test1(String state) {
        List<ICurrentStateDto> dtoList = new ArrayList<>();
        List<Student> studentList = studentDao.getAll();
        for (Student student : studentList) {
            StudentAdepter studentAdepter = new StudentAdepter(student);
            dtoList.add(studentAdepter);
        }
        return context.forwordState(FlowConstant.STUDY_FLOW_NAME, dtoList, state, null);
    }

    @RequestMapping("getAll")
    @ResponseBody
    public String getAll() {
        List<Student> all = studentDao.getAll();
        return JSONObject.toJSONString(all);
    }
}
